package com.ee.chat.admin.controller.systemmanage;

import com.alibaba.fastjson.JSON;
import com.ee.chat.admin.service.IAdminRoleService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.entity.AdminRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Resource
    IAdminRoleService adminRoleService;

    @RequestMapping("list")
    public Result list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit){
        try {
            return adminRoleService.list(request, page, limit);
        } catch (Exception e){
            log.error("【获取角色列表错误】", e);
        }
        return Result.returnFailure();
    }

    @RequestMapping("del")
    public Result del(HttpServletRequest request, @RequestParam("ids[]") List<Integer> ids){
        try {
            return adminRoleService.del(request, ids);
        } catch(Exception e){
            log.error("【删除角色错误】", e);
        }
        return Result.returnFailure();
    }

    @RequestMapping("toEdit")
    public Result toEdit(HttpServletRequest request, @RequestParam(required = false) Integer id){
        try {
            return adminRoleService.toEdit(request, id);
        } catch(Exception e){
            log.error("【编辑角色错误】", e);
        }
        return Result.returnFailure();
    }

    @RequestMapping("edit")
    public Result edit(HttpServletRequest request, AdminRole role){
        try {
            List<Integer> permissionIds = JSON.parseArray(role.getPermissionIds(), Integer.class);
            return adminRoleService.edit(request, role, permissionIds);
        } catch(Exception e){
            log.error("【保存角色错误】", e);
        }
        return Result.returnFailure();
    }

    @RequestMapping("add")
    public Result add(HttpServletRequest request, AdminRole role){
        try {
            List<Integer> permissionIds = JSON.parseArray(role.getPermissionIds(), Integer.class);
            return adminRoleService.save(request, role, permissionIds);
        } catch(Exception e){
            log.error("【保存角色错误】", e);
        }
        return Result.returnFailure();
    }

    @RequestMapping("changeStatus")
    public Result changeStatus(HttpServletRequest request, @RequestParam Integer id){
        try {
            return adminRoleService.changeStatus(request, id);
        } catch(Exception e){
            log.error("【变更管理员用户状态错误】", e);
        }
        return Result.returnFailure();
    }

}
